<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- row是行 -->
        <!-- 头部布局开始 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <!-- col是列 -->
          <el-col :span="20">
            <svg-icon icon-class="home" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>操作人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hadd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 头部布局结束 -->
        <el-tree
          :data="list"
          :props="{ children: 'children', label: 'name' }"
          @node-click="handleNodeClick"
        >
          <!-- 使用默认插槽 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <!-- col是列 -->
              <el-col :span="20">
                <svg-icon icon-class="home" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hadd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hremove(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        title="新增子部门"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <!-- 新增子部门或编辑部门的子组件 -->
        <addOrEdit
          v-if="showDialog"
          :id="curId"
          ref="myDept"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
      <!-- :origin-list="originList"  :属性名=属性值  父传子 -->
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListTreeList } from '@/utils/index'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      originList: [], // 保存数组
      showDialog: false, // 控制弹层的显示与隐藏
      list: [],
      curId: '', // 父级的id
      isEdit: false// false新增||true 编辑
    }
  },
  // created会在页面一开始就会发请求
  created() {
    this.lodaDepartments()
  },
  methods: {
    hClose() {
      console.log('弹窗关闭喽')
      this.$refs.myDept.resetFrom()
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true// 将编辑的状态改为true,确定当前是一个编辑操作
      this.showDialog = true
      // 解决数据不更新的方式2,在父组件中调用子组件
      // this.$nextTick(() => {
      //   this.$refs.myDept.lodaDepartmentsDetai()
      // })
    },
    hSuccess() {
      // 关闭弹幕,重新刷新页面
      this.showDialog = false
      this.lodaDepartments()
    },
    handleNodeClick() {},
    // 获取部门列表
    async lodaDepartments() {
      try {
        const res = await getDepartments()
        console.log(res, 'res')
        // 把数据添加到数组里面
        // this.list = res.data.depts
        this.originList = res.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        res.data.depts.shift()// 删除数组中第一条数据 公司名称
        this.list = tranListTreeList(res.data.depts)
        console.log(this.list)
      } catch (error) {
        console.log(error, '获取列表失败')
      }
    },
    hadd(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false// 将编辑状态改为false,变为新增操作
    },
    // 删除子部门
    hremove(id) {
      this.$confirm('确定删除该部门么, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        console.log(res, 'delDepartments')
        if (res.code === 10000) {
          this.lodaDepartments()
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
